app.controller('BusOrderDetailCtrl', function ($scope, $stateParams, ajax, toaster, $modal) {

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.orderId = $stateParams.orderId;
	$scope.status = $stateParams.status;
	$scope.tag = $stateParams.tag;
	$scope.id = $stateParams.id;
//	$scope.hide = $scope.status == 3 ? true : false;
//	$scope.sd = $scope.status == 9 ? true : false;

	//查询
	$scope.reload = function () {
		ajax.call(new Req.GET({
			resource: 'order/bs/' + $scope.orderId + '/id'
		})).then(function (result) {
			$scope.order = result.result;
			$scope.shiftId=result.result.subOrders[0].product.shiftId;
			for (var i = 0; i < $scope.order.subOrders.length; i++) {
				if($scope.order.subOrders[i].status==9){
					$scope.sd=true;
				}
				if($scope.order.subOrders[i].status==3){
					$scope.hide=true;
				}
			}
		}, function () { });
	};

	//查询订单支付方式
	$scope.reloadPay = function () {
		ajax.call(new Req.GET({
			resource: 'order/' + $scope.orderId + '/payname'
		})).then(function (result) {
			$scope.payname = result.result;
		}, function () { });
	};
	if ($scope.orderId) {
		$scope.reload();
		$scope.reloadPay();
	}

	$scope.ids = [];
	$scope.checks = false;//默认未选中
	$scope.ckAll = false;//默认未选中
	$scope.choseArr = [];
	$scope.flag = false;

	/*单选*/
	$scope.check = function (val) {
		if ($scope.ids.indexOf(val) < 0) {
			$scope.ids.push(val);
			$scope.flag = $scope.ids != undefined && $scope.ids != null && $scope.ids != "";
		} else {
			$scope.ids.splice($scope.ids.indexOf(val), 1);
			$scope.flag = $scope.ids != undefined && $scope.ids != null && $scope.ids != "";
		}
	};
	
	$scope.list=[];
	$scope.show=false;
	/*单选*/
	$scope.checkSd = function (val) {
		if ($scope.list.indexOf(val) < 0) {
			$scope.list.push(val);
			$scope.show = $scope.list != undefined && $scope.list != null && $scope.list != "";
		} else {
			$scope.list.splice($scope.list.indexOf(val), 1);
			$scope.show = $scope.list != undefined && $scope.list != null && $scope.list != "";
		}
	};

	$scope.count = 0;
	$scope.ok = function (order) {
		inserter.datalist = $scope.ids;
		inserter.resource = 'order/bs/'  + order.passengerId + '/' + order.id + '/prefund';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				$scope.prefund = result.result;
				if ($scope.prefund.success) {
					var message = $scope.prefund.message;
					$scope.json = angular.fromJson(message);
					var modalInstance = $modal.open({
						templateUrl: 'template/bus/order/module.html?v=8fcfa8bd26',
						controller: 'BusOrderModalCtrl',
						size: 'sm',
						resolve: {
							param: function () {
								return {
									"type": "refund",
									"charge": $scope.json.charge,
									"refund": $scope.json.refund
								};
							}
						}
					});

					modalInstance.result.then(function () {
						inserter.datalist = $scope.ids;
						inserter.resource = 'order/bs/' + order.passengerId + '/' + order.id + '/refund';
						ajax.call(inserter).then(function (result) {
							if (result.code == 200) {
								if (result.result.success) {
									toaster.pop('success', '成功', '申请退票成功。');
									$scope.reload();
								} else {
									toaster.pop('error', '错误', result.result.message);
								}
							} else {
								toaster.pop('error', '错误', '申请退票失败！');
							}
						}, function () {
							toaster.pop('error', '错误', '申请退票失败！');
						});
					}, function () { });
				} else {
					toaster.pop('info', '提示', $scope.prefund.message);
				}
			}
		}, function () {
			toaster.pop('error', '错误', '申请退票失败！');
		});
	};

	$scope.refund = function (order) {
		if ($scope.list[0] != null) {
			inserter.datalist = $scope.list;
			inserter.resource = 'order/bs/'  + order.passengerId + '/' + order.id + '/bsrefund';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					$scope.prefund = result.result;
					if ($scope.prefund.success) {
						var message = $scope.prefund.message;
						$scope.json = angular.fromJson(message);
						var modalInstance = $modal.open({
							templateUrl: 'template/bus/order/module.html?v=8fcfa8bd26',
							controller: 'BusOrderModalCtrl',
							resolve: {
								param: function () {
									return {
										"type": "update",
										"charge": $scope.json.charge,
										"refund": $scope.json.refund
									};
								}
							}
						});

						modalInstance.result.then(function (param) {
							inserter.fields = param;
							inserter.datalist = $scope.list;
							inserter.resource = 'order/bs/' + order.id + '/update';
							ajax.call(inserter).then(function (result) {
								if (result.code == 200) {
									$scope.reload();
									$scope.list = [];
									$scope.show = false;
									toaster.pop('success', '成功', '手动退票成功。');
								} else {
									toaster.pop('error', '错误', result.result);
								}
							}, function () {
								toaster.pop('error', '错误', '手动退票失败！');
							});
						}, function () { });
					} else {
						toaster.pop('info', '提示', $scope.prefund.message);
					}
				}
			}, function () {
				toaster.pop('error', '错误', '申请退票失败！');
			});
		} else {
			toaster.pop('warning', '提示', '请选择车票！');
		}
	};

	$scope.barcodeOpen = function (order) {
		$modal.open({
			templateUrl: 'template/bus/order/qrcode.html?v=2a3acbcf8e',
			controller: 'BusQrCcodeCtrl',
			resolve: {
				param: function () {
					return {
						"orderNo": order.orderNo,
						"barcode": order.orderDetail.qrcode
					};
				}
			}
		});
	};
});